<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="classifyName">
              <el-input v-model="filters.classifyName" clearable placeholder="分类名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增 </el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table ref="table" :data="dataList" :fit="true" highlight-current-row v-loading="listLoading">
        <el-table-column align="center" label="序号" type="index" width="80"></el-table-column>
        <el-table-column align="center" label="分类名称" prop="classifyName" width="200"> </el-table-column>
        <el-table-column align="center" label="分类Logo" width="120">
          <template slot-scope="scope">
            <el-image title="点击可预览" :src="scope.row.classifyLogo" fit="cover" :preview-src-list="[scope.row.classifyLogo]" :z-index="scope.$index"> </el-image>
          </template>
        </el-table-column>
        <el-table-column align="center" label="类型" prop="classifyType" width="200" :formatter="formatClassifyType"> </el-table-column>
        <el-table-column align="center" label="分类描述" prop="classifyContent" width="200"> </el-table-column>
        <el-table-column align="center" width="100" label="创建人" prop="createUserName"> </el-table-column>
        <el-table-column align="center" width="160" label="创建时间" prop="createDate"> </el-table-column>
        <el-table-column align="center" width="100" label="修改人" prop="modifyUserName"> </el-table-column>
        <el-table-column align="center" width="160" label="修改时间" prop="modifyDate"> </el-table-column>
        <el-table-column align="center" width="100" label="状态">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="plain" :type="formatStates(scope.row.state)">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)"> {{ scope.row.state === 0 ? '启用' : '禁用' }}</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import TopClassifyApi from '@/api/server/topClassify';
import * as message from '@/constants/message';

export default {
  name: 'TopClassifyManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      loadingIns: false,
      listLoading: true,
      filters: {
        classifyName: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

      permissionAdd: 'SYSTEM_MANAGER_SERVER_TOP_CLASSIFY_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_SERVER_TOP_CLASSIFY_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_SERVER_TOP_CLASSIFY_LIST_EDIT',
      permissionUpStatus: 'SYSTEM_MANAGER_SERVER_TOP_CLASSIFY_LIST_STATE',
      permissionDelete: 'SYSTEM_MANAGER_SERVER_TOP_CLASSIFY_LIST_DELETE',
    };
  },
  computed: {},
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  components: {},
  methods: {
    formatClassifyType(row, column, cellValue) {
      switch (cellValue) {
        case 1:
          return '大屏生活类';
        case 2:
          return '大屏医疗类';
      }
    },
    formatState(cellValue) {
      if (cellValue === -1) {
        return '删除';
      } else if (cellValue === 0) {
        return '禁用';
      } else if (cellValue === 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue === -1) {
        return 'danger';
      } else if (cellValue === 0) {
        return 'warning';
      } else if (cellValue === 1) {
        return 'success';
      }
    },
    init() {
      this.listLoading = true;
      this.listData();
      this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listData();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listData();
    },
    handleClickAdd() {
      this.$router.push({
        path: '/server/topClassifyAdd',
      });
    },
    handleClickEditor(row) {
      this.$router.push({
        path: '/server/topClassifyEdit',
        query: { id: row.topClassifyId },
      });
    },
    handleClickDetail(row) {
      this.$router.push({
        path: '/server/topClassifyDetail',
        query: { id: row.topClassifyId },
      });
    },
    handleIsDisable(row) {
      let currentState = row.state === 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        TopClassifyApi.deleteTopClassify({
          id: row.topClassifyId,
          state: currentState,
        }).then((res) => {
          this.$Utils.notifySuccess();
          _this.listData();
        });
      });
    },
    handleClickDelete(val) {
      let _this = this;
      _this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        TopClassifyApi.deleteTopClassify({
          id: val.topClassifyId,
          state: -1,
        }).then((res) => {
          this.$Utils.notifySuccess();
          _this.listData();
        });
      });
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    async listData() {
      const {
        filters: { classifyName },
        page: { pageNo, pageSize },
      } = this;
      let cond = {
        classifyName,
        pageNo,
        pageSize,
      };
      try {
        const { data } = await TopClassifyApi.listTopClassify(cond);
        this.page.pageTotal = data.total;
        this.dataList = data.list || [];
        this.$refs.table.doLayout();
      } finally {
        this.listLoading = false;
      }
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
